var main=[[ //表头
	{type:'checkbox', fixed: 'left'},
	{field: 'car_auction_id', title: '购车申请单号',  sort: true, fixed: 'left'}
    ,{field: 'number', title: '车牌号',  sort: true}
    ,{field: 'brand', title: '品牌'} 
    ,{field: 'car_type_name', title: '车型'}
    ,{field: 'company_name', title: '公司'}
    ,{field:'state2', title:'状态'}
   	,{ title: '操作',width:250, toolbar:'#operation',fixed: 'right'}
]];
var auto='';
var fi=1;
var page=1;
var data = {};
data.page = page;
data.pagenum = 10;
var where_json = {};
	{if $Think.get.car_audit_id}
		where_json.car_audit_id=['like',"%{$Think.get.car_audit_id}%"];
	{/if}
	{if $Think.get.company_name}
		where_json.company_name=['like',"%{$Think.get.company_name}%"];
	{/if}
	{if $Think.get.number}
		where_json.number=['like',"%{$Think.get.number}%"];
	{/if}
	{if $Think.get.state}
		where_json.state=['in',"{$Think.get.state}"];
	{/if}
	data.where_json = JSON.stringify(where_json)
	ajaxauction(data)
	function ajaxauction(data) {
		Load()
	ajax(api.cycle_auction.list, data, 'post').done(function(res) {
		console.log(res)
		Load(1)
		if(res.code == 1) {
			for(var stateidx = 0; stateidx < res.data.length; stateidx++) {
				if(res.data[stateidx].state == 1) {
					res.data[stateidx].state2 = '待评估'
				} else if(res.data[stateidx].state == 2) {
					res.data[stateidx].state2 = '待审核'
				} else if(res.data[stateidx].state == 3) {
					res.data[stateidx].state2 = '评估不通过'
				} else if(res.data[stateidx].state == 4) {
					res.data[stateidx].state2 = '已审核'
				} else if(res.data[stateidx].state == 5) {
					res.data[stateidx].state2 = '审核不通过'
				} else if(res.data[stateidx].state == 6) {
					res.data[stateidx].state2 = '已登记'
				} else if(res.data[stateidx].state == 7) {
					res.data[stateidx].state2 = '复审'
				} else if(res.data[stateidx].state == 8) {
					res.data[stateidx].state2 = '已拍卖'
				}
			}
			table.render({
    				elem: '#buy',
    				cellMinWidth: 80,
    				cols: main,
    				data: res.data,
    				limit: data.pagenum,
    				even: true
    			});
    			if (fi == 1) {
    				laypage.render({
    					elem: 'page',
    					count: res.count,
						layout: ['count', 'prev', 'page', 'next', 'limit', 'refresh', 'skip'],
						theme: '#1E9FFF',
    					jump: function (obj, first) {
    						if (!first) {
    							page = obj.curr
    							data.page = page
    							pagenum = obj.limit
    							data.pagenum = pagenum
    							ajaxbuycar(data)
    						} else {

    						}
    					}
    				});
    				fi = 2
    			}
    		table.on('tool(buy)', function(obj) {
				var data1 = obj.data; //获得当前行数据
				var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
				var tr = obj.tr; //获得当前行 tr 的DOM对象
				if(layEvent === 'detail') {
					detailauction(data1)
				}else if(layEvent === 'auction_assess'){
					assess(data1)
				}else if(layEvent === 'auction_check'){
					auctioncheck(data1)
				}else if(layEvent === 'auction_resgister'){
					auctionresgister(data1)
				}else if(layEvent === 'second_check'){
					secondcheck(data1)
				}
			})
    		
			
			
			
		} else {
			Err('请求失败')
		}
	}).fail(

	)
}
$('.add').on('click',function(res){
	propcheck()
})
	//申请
	function propcheck(obj,main){
		var auto = layer.open({
   		type: 1,
   		title:'车辆拍卖申请',
   		skin: 'layui-layer-rim', //加上边框
   		area: ['650px', '350px'], //宽高
   		content: `
		   <div class='layui-row' style='padding:10px'>
		   <form class="layui-form">
			   <div class='layui-col-sm12'>
				   <div  style='margin-bottom:10px'>
					   <div class="layui-form-item " style="padding-right:10px">
					   <div class="layui-input-inline">
						   <input type="hidden" class="car_id" name="car_id" value="{$item.car_id}" />
						<input type="text" class="layui-input number" value="{$item.car_number}" name="car_number" data-check='require' data-msg="车牌号不能为空" readonly="" />
					   </div>
					   <div class='am-btn-group am-fl am-btn-group-xs cl-p' >
						   <button type="button" class='layui-btn layui-btn-sm  car_detail'>查看车辆详情</button>
					   </div>
					   </div>
				   </div>
				   <div >
					   <div class='layui-col-sm12'>
						   <textarea style='height:150px;width:100%' class='why' placeholder='请填写拍卖理由'></textarea>
					   </div>
				   </div>
				   
			   </div>
			   
			   <div class='layui-col-sm12' style="text-align:center;margin-top:10px">
					   <button type='button' class='layui-btn apply_ok'>提交申请</button>
					   <button type='button' class='layui-btn apply_no'>取消</button>
			   </div>
		   </form>
		   </div>
   		`,
   		success:function(){
   			$('.number').on('click',function(){
				opsearch("{:url('/html/car/car')}?op=search")
			});
   			$('.car_detail').on('click',function(){
				   console.log($('.car_id').val()=="")
   				if($('.car_id').val()==""){
					Err('请选择车辆')
				}else{
					window.open("{:url('html/car/car_detail')}?car_id="+$('.car_id').val());  
//					choosecar("{:url('detail')}?car_id="+$('.CarId').val(),'车辆详情')
	   			}
   			})
   			$('.apply_ok').on('click',function(){
	   			Load()
	   			ajax(api.cycle_auction.add,{
	   				car_id:$('.car_id').val(),
	   				why:$('.why').val()
	   			},'post').done(function(res){
	   				Load(1)
	   				if(res.code==1){
	   					console.log(res)
						Msg('已提交申请')
						ajaxauction(data)
						layer.close(auto)
					}else{
						Err('请求失败')
					}
	   			}).fail(function(){
	   				Load(1)
	   				Err('请求失败')
	   			})
	   			
	   			})
	   			$('.apply_no').on('click',function(){
	   				layer.close(auto)
	   			})
   		}
   		})
	}
	//删除	
	function scrapclose(obj,main){
		auto = layer.open({
	   		type: 1,
	   		title:false,
	   		skin: 'layui-layer-rim', //加上边框
	   		area: ['200px', '100px'], //宽高
	   		content: `
	   			<div class='layui-col-sm12' style='padding:10px'>
	   				<div class='layui-col-sm12' style='text-align:center;margin-bottom:10px'>是否删除该报废单</div>
	   				<div class='layui-col-sm12' style='text-align:center'>
	   					<a class='am-btn am-btn-sm am-btn-primary am-radius close_ok'>是</a>
	   					<a class='am-btn am-btn-sm am-btn-primary am-radius close_no'>否</a>
	   				</div>
	   			</div>
	   		`,
	   		success:function(){
	   			
	   			$('.close_ok').on('click',function(){
	   				Load()
	   				$.ajax({
	   					type:'post',
	   					data:{
	   						car_auction_id:obj.closest('tr').data('id')
	   					},
	   					url:url.delete,
	   					success:function(res){
	   						Load(1)
	   						console.log(res)
	   						if(res.code==1){
	   							ajaxauction(data)
	   							Msg('删除成功')
	   						}
	   						layer.close(auto)
	   					},
	   					error:function(){
							Load(1)
	   						Err('请求失败')
	   						layer.close(auto)
	   					}
	   				})
	   			});
	   			$('.close_no').on('click',function(){
	   				layer.close(auto)
	   			})
	   		}
	   	})
	}
	//评估
	function assess(main){
		console.log(main)
		var auto = layer.open({
   		type: 1,
   		title:'拍卖评估',
   		skin: 'layui-layer-rim', //加上边框
   		area: ['500px', '180px'], //宽高
   		content: `
   			<div class='layui-col-sm12' style='padding:10px'>
   				<div style='text-align:center'>评估<a style="color:#1E9FFF" href='{:url("detail")}?_blank=1'  target="_blank">${main.number}</a>是否可以进行拍卖</div>
	   			<div class='layui-col-sm12' style="text-align:center;margin-top:10px">
   					<button class='layui-btn assess_ok'>通过</button>
   					<button class='layui-btn assess_no'>不通过</button>
	   			</div>
		   		
   			</div>
   		`,
   		success:function(){
   			
   			$('.assess_ok').on('click',function(){
   				Load()
   				$.ajax({
   					type:'post',
   					data:{
   						car_auction_id:main.car_auction_id,
   						state:2
   					},
   					url:api.cycle_auction.check,
   					success:function(res){
   						Load(1)
   						console.log(res)
   						Msg('评价通过')
   						ajaxauction(data)
   						layer.close(auto)
   					},
   					error:function(){
   						Load(1)
   						Err('请求失败')
   					}
   				})
				
   			})
   			$('.assess_no').on('click',function(){
   				Msg('评估不通过')
   				obj.closest('.operate').parent().prev().html('评估不通过')
				obj.remove()
				layer.close(auto)
   			})
   		}
   		})
	}
	//审核
	function auctioncheck(main){
		auto = layer.open({
   		type: 1,
   		title:'拍卖申请审核',
   		skin: 'layui-layer-rim', //加上边框
   		area: ['500px', '400px'], //宽高
   		content: `
   			<div class='layui-row' style='padding:10px;font-size:16px'>
	   			<form class="layui-form" enctype="multipart/form-data" data-check="1" data-ajax="1" id="scrap1">
		   			<div class='layui-col-sm12'>
		   				<div class="layui-form-item cl-m">
		   				 	<label class="layui-form-label">车牌号:</label>
		                    <div class="layui-inline" style='padding:9px 0'>
		                       	${main.number}
		                    </div>
		   					<div class="layui-inline">
		   						<button type="button" class='layui-btn layui-btn-xs car_detail'>查看车辆详情</button>
		   					</div>
			   			</div>
		   				<div class="layui-form-item cl-m">
		   				 	<label class="layui-form-label">申请人:</label>
		                    <div class="layui-inline" style='padding:9px 0'>
		                       ${main.people?main.people:'-'}
		                    </div>
		   				</div>
		   				<div class="layui-form-item cl-m">
		   				 	<label class="layui-form-label">申请时间:</label>
		                    <div class="layui-inline" style='padding:9px 0'>
		                       	${main.date}
		                    </div>
		   				</div>
		   				<div class="layui-form-item cl-m">
		   				 	<label class="layui-form-label">拍卖原因:</label>
		                    <div class="layui-inline" style='padding:9px 0'>
		                       ${main.why}
		                    </div>
		   				</div>
		   			</div>
		   			<div class='layui-col-sm12' style="text-align:center;margin-top:10px">
	   					<a class='layui-btn layui-btn-sm scrap_ok'>同意拍卖</a>
	   					<a class='layui-btn layui-btn-sm scrap_no'>拒绝拍卖</a>
		   			</div>
		   		</form>
   			</div>
   		`,
   		success:function(){
   			$('.car_detail').on('click',function(){
   				window.open('{:url("/html/car/car_detail")}?car_id='+main.car_id)
   			})
   			$('.scrap_ok').on('click',function(){
   				Load()
   				$.ajax({
   					type:'post',
   					data:{
   						car_auction_id:main.car_auction_id,
   						state:4
   					},
   					url:api.cycle_auction.check,
   					success:function(res){
   						Load(1)
   						Msg('审核通过')
   						ajaxauction(data)
   						layer.close(auto)
   					},
   					error:function(){
   						Load(1)
   						Err('请求失败')
   					}
   				})
   			})
   			$('.scrap_no').on('click',function(){
   				Load()
   				$.ajax({
   					type:'post',
   					data:{
   						car_auction_id:main.car_auction_id,
   						state:5
   					},
   					url:api.cycle_auction.check,
   					success:function(res){
   						Load(1)
   						Msg('拒绝拍卖')
   						ajaxauction(data)
   						layer.close(auto)
   					},
   					error:function(){
   						Load(1)
   						Err('请求失败')
   					}
   				})
   				
   				
   				layer.close(auto)
   			})
   		}
   		})
	}
	//登记
	function auctionresgister(main){
		var auto = layer.open({
   		type: 1,
   		title:'车辆拍卖登记',
   		skin: 'layui-layer-rim', //加上边框
   		area: ['650px', '480px'], //宽高
   		content: `
   			<div class='layui-row' style='padding:10px'>
   			<form class="layui-form" enctype="multipart/form-data" data-check="1" data-ajax="1" id="scrap2">
	   			<div class='layui-col-sm12'>
   					<div class="layui-form-item cl-m">
	   				 	<label class="layui-form-label">车牌号:</label>
	                    <div class="layui-inline" style='padding:9px 0'>
	                       	${main.number}
	                    </div>
	   					<div class="layui-inline">
	   						<button type="button" class='layui-btn layui-btn-xs car_detail'>查看车辆详情</button>
	   					</div>
		   			</div>
	   				<div class="layui-form-item cl-m">
	   				 	<label class="layui-form-label">成交金额:</label>
	                    <div class="layui-inline" >
	                       	<input type='number' class='auction_money layui-input'/>
	                    </div>
	   				</div>
	   				<div class="layui-form-item cl-m">
	   				 	<label class="layui-form-label">成交方:</label>
	                    <div class="layui-inline" >
	                       <input type='text' class='auction_er layui-input'/>
	                    </div>
	   				</div>
	   				
	   				<div class="layui-form-item cl-m">
	   				 	<label class="layui-form-label">上传图片:</label>
	                    <div class="layui-inline" >
	                        <div class="layui-upload">
                                
                                <div class="layui-upload-list">
                                    <img data-name='head_img' class="layui-upload-img" id="img66">
                                    <p id="demoText"></p>
                                </div>
                                <button type="button" class="layui-btn" id="test66">上传图片</button>
                            </div>
	                    </div>
	   				</div>
	   			</div>
	   			
	   			<div class='layui-col-sm12' style="text-align:center;margin-top:10px">
   					<a class='layui-btn layui-btn-sm scrap_ok2'>登记</a>
   					<a class='layui-btn layui-btn-sm scrap_no2'>取消</a>
	   			</div>
	   		</form>
   			</div>
   		`,
   		success:function(){
   			var imgpath={};
   			upImage('{:url("/adminapi/Upload/uploadImg")}', '#test66', '#img66', imgpath,'image')
   			$('.car_detail').on('click',function(){
   				window.open("{:url('html/car/car_detail')}?_blank=1&car_id="+main.car_id)
   			})
   			
   			
   			
   			$('.scrap_ok2').on('click',function(){
   				console.log(imgpath)
   				Load()
   				$.ajax({
   					type:'post',
   					data:{
   						car_auction_id:main.car_auction_id,
   						state:6,
   						true_money:$('.auction_money').val(),
   						auction_er:$('.auction_er').val(),
   						scrap_image:imgpath.image

   					},
   					url:api.cycle_auction.check,
   					success:function(res){
   						Load(1)
   						console.log(res)
   						Msg('已确认')
   						ajaxauction(data)
   						layer.close(auto)
   					},
   					error:function(){
   						Load(1)
   						Err('请求失败')
   					}
   				})
   					
// 				
   			})
   			$('.scrap_no2').on('click',function(){
   				
				layer.close(auto)
   			})
   		}
   		})
	}
	//二次审核
	function secondcheck(main){
		var detail_cont=`<div class='layui-row' style='padding:10px;font-size:14px;height:100%'>
	   			<div class='layui-col-sm12 cl-p'  style='height:100%'>
	   				<div class='layui-col-sm6 cl-p'>
		   				<div class="layui-col-sm12 cl-p">
			   				<div class="m-label-sm">车牌号:</div>
							<div data-value="1" class="m-value-sm">${main.number} </div>
	   					</div>
			   			
		   				<div class="layui-col-sm12 cl-p">
		   					<div class="m-label-sm">拍卖申请人:</div>
							<div data-value="1" class="m-value-sm">${main.people?main.people:'-'}</div>
		   				</div>
			   			<div class="layui-col-sm12 cl-p">
			   				<div class="m-label-sm">申请时间:</div>
							<div data-value="1" class="m-value-sm">${main.date}</div>
			   			</div>
			   			
			   			<div class="layui-col-sm12 cl-p">
			   				<div class="m-label-sm">审核时间:</div>
							<div data-value="1" class="m-value-sm">${main.date_2}</div>
			   			</div>
			   			<div class="layui-col-sm12 cl-p">
			   				<div class="m-label-sm">拍卖成交方:</div>
							<div data-value="1" class="m-value-sm">${main.auction_er}</div>
			   			</div>
		   				<div class="layui-col-sm12 cl-p">
		   					<div class="m-label-sm">拍卖时间:</div>
							<div data-value="1" class="m-value-sm">${main.date_4}</div>
		   				</div>
		   				
		   				<div class="layui-col-sm12 cl-p">
		   					<div class="m-label-sm">登记时间:</div>
							<div data-value="1" class="m-value-sm">${main.date_6}</div>
		   				</div>
			   			<div class="layui-col-sm12 cl-p">
			   				<div class="m-label-sm">成交金额:</div>
							<div data-value="1" class="m-value-sm">${main.true_money}</div>
			   			</div>
			   			<div class="layui-col-sm12 cl-p">
			   				<div class="m-label-sm">拍卖理由:</div>
							<div data-value="1" class="m-value-sm">${main.why}</div>
			   			</div>
		   			</div>
		   			<div class='layui-col-sm6' style='height:80%;'>
		   				<img class='magnifyauction' src='{$static_dir}/assets/img/noimg.gif' alt="拍卖凭证" data-magnify='1' style='height:100%;width:100%;border:1px solid #999'>
		   			</div>
	   				<div class='layui-col-sm12' style='text-align: center;margin-top:10px'>
	   					<button class='layui-btn layui-btn-sm second_check'>审核</button>
	   					<button class='layui-btn layui-btn-sm '>取消</button>
	   				</div>
				</div>
   			</div>`
		auto = layer.open({
	   		type: 1,
	   		title:'车辆复审',
	   		skin: 'layui-layer-rim', //加上边框
	   		area: ['600px', '400px'], //宽高
	   		content: detail_cont,
	   		success:function(){
	   			$('.car_detail').on('click',function(){
	   				choosecar("{:url('detail')}?_blank=1&car_id="+$('.CarId').val(),'车辆详情')
	   			})
	   			$('.second_check').on('click',function(){
	   				Load()
		   				$.ajax({
		   					type:'post',
		   					data:{
		   						car_auction_id:main.car_auction_id,
		   						state:8
		   					},
		   					url:api.cycle_auction.check,
		   					success:function(res){
		   						Load(1)
		   						console.log(res)
		   						Msg('复审通过')
		   						ajaxauction(data)
		   						layer.close(auto)
		   					},
		   					error:function(){
		   						Load(1)
		   						Err('请求失败')
		   					}
		   				})
	   			})
	   			$('.magnifyauction').on('click',function(){
				if($(this).data('magnify')){
					console.log($(this).attr('src'))
					layer.open({
					  type: 1,
					  title:false,
					  skin: 'layui-layer-rim', //加上边框
					  area: ['500px', '500px'], //宽高
					  content: `<img style="width: 100%;height:100%;" src="${$(this).attr('src')}" />`
					});
				}
			});
	   		}
	   	})
	}
	
	function detailauction(main){
		console.log(main)
		var detail_cont=`
			<div class='layui-row' style='padding:10px;font-size:14px;height:100%'>
	   			<div class='layui-col-sm6 cl-p'  style='height:100%'>
	   				<div class="layui-col-sm12 cl-p">
		   				<div class="m-label-sm">车牌号:</div>
						<div data-value="1" class="m-value-sm">${main.number}</div>
		   			</div>
		   			<div class="layui-col-sm12 cl-p">
		   				<div class="m-label-sm">车型:</div>
						<div data-value="1" class="m-value-sm">${main.car_type_name}</div>
		   			</div>
	   				<div class="layui-col-sm12 cl-p">
		   				<div class="m-label-sm">品牌:</div>
						<div data-value="1" class="m-value-sm">${main.brand}</div>
		   			</div>
		   			<div class="layui-col-sm12 cl-p">
		   				<div class="m-label-sm">状态:</div>
						<div data-value="1" class="m-value-sm">${main.state2}</div>
		   			</div>
		   			<div class="layui-col-sm12 cl-p">
		   				<div class="m-label-sm">状态:</div>
						<div data-value="1" class="m-value-sm">${main.state2}</div>
		   			</div>
		   			<div class="layui-col-sm12 cl-p">
		   				<div class="m-label-sm">申请时间:</div>
						<div data-value="1" class="m-value-sm">${main.date}</div>
		   			</div>`
	   			
		if(main.state>=2){
			detail_cont+=`
					<div class="layui-col-sm12 cl-p">
		   				<div class="m-label-sm">评估时间:</div>
						<div data-value="1" class="m-value-sm">${main.date_2}</div>
		   			</div>
			`
			if(main.state>=4){
				detail_cont+=`
					<div class="layui-col-sm12 cl-p">
		   				<div class="m-label-sm">审核时间:</div>
						<div data-value="1" class="m-value-sm">${main.date_4}</div>
		   			</div>
			`
				if(main.state>=6){
				detail_cont+=`
					<div class="layui-col-sm12 cl-p">
		   				<div class="m-label-sm">登记时间:</div>
						<div data-value="1" class="m-value-sm">${main.date_6}</div>
		   			</div>
				`
					if(main.state>=8){
					detail_cont+=`
						<div class="layui-col-sm12 cl-p">
			   				<div class="m-label-sm">复审时间:</div>
							<div data-value="1" class="m-value-sm">${main.date_8}</div>
			   			</div>
					`
					}
				}
			}
		}
		detail_cont+=`
			
			</div>
			<div class='layui-col-sm6 cl-p'  style='height:100%'>
				<img src='${main.auction_image}' style='height:280px;width:100%'>
			</div>
	   	</div>
		`
		var auto = layer.open({
   		type: 1,
   		title:'车辆拍卖详情',
   		skin: 'layui-layer-rim', //加上边框
   		area: ['600px', '400px'], //宽高
   		content: detail_cont,
   		success:function(){
   			$('.car_detail').on('click',function(){
   				choosecar("{:url('detail')}?_blank=1&car_id="+$('.CarId').val(),'车辆详情')
   			})
   			$('.magnifyauction').on('click',function(){
			if($(this).data('magnify')){
				console.log($(this).attr('src'))
				layer.open({
				  type: 1,
				  title:false,
				  skin: 'layui-layer-rim', //加上边框
				  area: ['500px', '500px'], //宽高
				  content: `<img style="width: 100%;height:100%;" src="${$(this).attr('src')}" />`
				});
			}
		});
   		}
   		})
	}
//})